package com.story.modular.app.controller.radio;

import com.story.common.annotion.BussinessLog;
import com.story.common.constant.Dict;
import com.story.common.constant.factory.ConstantFactory;
import com.story.common.exception.BizExceptionEnum;
import com.story.common.exception.BussinessException;
import com.story.common.persistence.dao.BannerMapper;
import com.story.common.persistence.model.Banner;
import com.story.config.properties.UpyunProperties;
import com.story.core.base.controller.BaseController;
import com.story.core.log.LogObjectHolder;
import com.story.core.util.ToolUtil;
import com.story.core.util.UriUtil;
import com.story.modular.app.dao.BannerDao;
import com.story.modular.app.dao.VedioCategoryDao;
import com.story.modular.app.service.UpyunService;
import com.story.modular.app.warpper.BannerWarpper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @decription 音频栏目管理
 * @Date 2017-12-19 15:25
 */
@Api(value = "音频分类栏目管理", description = "音频分类栏目管理")
@Controller
@RequestMapping(value = "/banner/radio")
public class RadioBannerController extends BaseController{

    private String PREFIX = "/app/banner/radio/";

    private static final Long EXPIRE_TIME = 3600L;

    @Autowired
    private BannerDao bannerDao;

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private VedioCategoryDao vedioCategoryDao;

    @Autowired
    private UpyunService upyunService;

    @Autowired
    private UpyunProperties upyunProperties;

    /**
     * 进入banner页面
     * @return
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "banner.html";
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @RequestMapping(value = "/bannerAdd")
    public String bannerAdd() {
        return PREFIX + "banner_add.html";
    }

    /**
     * 修改跳转
     * @param bannerId
     * @param model
     * @return
     */
    @RequestMapping(value = "/bannerUpdate/{bannerId}")
    public String bannerUpdate(@PathVariable(value = "bannerId") Integer bannerId,
                              Model model) {
        Banner banner = bannerDao.selectBannerById(bannerId);
        String iconPath = banner.getIconPath();
        //得到带token的url
        banner.setIconPath(upyunProperties.getDomain()+iconPath+"?_upt="+upyunService.getTokenSign(UriUtil.getUri(iconPath),EXPIRE_TIME));
        model.addAttribute("pName", ConstantFactory.me().getBannerName(bannerId));
        model.addAttribute(banner);
        LogObjectHolder.me().set(banner);
        return PREFIX + "banner_update.html";
    }

    /**
     * 栏目列表
     * @param condition
     * @return
     */
    @ApiOperation(value="音频分类列表", notes="音频分类列表",httpMethod = "POST")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        //type = 1表示为视频资源
        Integer type = 0;
        List<Map<String,Object>> list = bannerDao.bannerList(condition,type);
        list.stream().forEach(map -> map.put("iconPath",upyunProperties.getDomain() + map.get("iconPath")));
        return super.warpObject(new BannerWarpper(list));
    }

    /**
     * 详情
     * @param bannerId
     * @return
     */
    @RequestMapping(value = "/detail/{bannerId}")
    @ResponseBody
    public Object detail(@PathVariable("bannerId") Integer bannerId) {
        return bannerDao.selectBannerById(bannerId);
    }

    /**
     * 新增栏目
     * @param banner
     * @return
     */
    @BussinessLog(value = "新增栏目", key = "name", dict = Dict.BannerDict)
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Banner banner) {
        if (ToolUtil.isOneEmpty(banner, banner.getName())) {
            throw new BussinessException( BizExceptionEnum.REQUEST_NULL);
        }
        int count = bannerDao.countBannerByName(banner.getName());
        if(count > 0) {
            throw new BussinessException(BizExceptionEnum.BANNER_EXIST);
        }
        banner.setIconPath(UriUtil.getUri(banner.getIconPath().split("\\?")[0]));
        //增加栏目的资源类型
        banner.setType(0);
        return bannerMapper.insert(banner);
    }

    /**
     * 修改
     * @param banner
     * @return
     */
    @BussinessLog(value = "修改栏目", key = "id", dict = Dict.BannerDict)
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Banner banner) {
        if(ToolUtil.isEmpty(banner) || banner.getId() == null) {
            throw new BussinessException( BizExceptionEnum.REQUEST_NULL);
        }
        banner.setIconPath(UriUtil.getUri(banner.getIconPath().split("\\?")[0]));
        banner.setType(0);
        bannerMapper.updateById(banner);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除
     * @param bannerId
     * @return
     */
    @BussinessLog(value = "删除栏目", key = "id", dict = Dict.DeleteDict)
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(Integer bannerId) {
        if(vedioCategoryDao.countCategoryByBanner(bannerId) > 0) {
            throw new BussinessException(BizExceptionEnum.BANNER_CATEGORY_EXIST);
        }
        bannerMapper.deleteById(bannerId);
        return SUCCESS_TIP;
    }

}
